// pages/bianji/bianji.js
const app = getApp()

var api = require('../../../js/api.js');
var utilH = require('../../../js/utilHeart.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {

		user: {
				avatarUrl: "",
				birthday: '2000-01-01',
				gender: '1',
				height: '',
				nickName: '',
				userWxId: 0,
				weight: '',
				wxInfoId: 0,

			}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this

		that.setData({
			user: app.globalData.users[0],

		})

		console.log(that.data.user)

		that.setData({
			'user.birthday': that.data.user.birthday != null ? that.data.user.birthday.substr(0,10):'2000-01-01'
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	bindDateChange: function(e) {
		var that = this
		console.log('picker发送选择改变，携带值为', e.detail.value)
		that.setData({
			'user.birthday': e.detail.value
		})
	},
	radioChange: function(e) {
		var that = this
		console.log('radio发生change事件，携带value值为：', e.detail.value)
		that.setData({
			'user.sex': e.detail.value
		})
	},

	formSubmit: function(e) {
		var that = this
		console.log(that.data.user)
		console.log('form发生了submit事件，携带数据为：', e.detail.value)

		that.setData({
			'user.wxNickname':e.detail.value.wxNickname,
			'user.wxSex':e.detail.value.wxSex,
			'user.birth':e.detail.value.birth,
			'user.height':e.detail.value.height,
			'user.weight':e.detail.value.weight,

		})



		let msg = ''
		var RegExp = /^\d+$/;
		if(that.data.user.wxNickname ==''){
			msg='昵称不能为空'
		}else if(that.data.user.wxNickname.length>10){
			msg='昵称长度超限'
		}else if(that.data.user.wxSex ==''){
			msg='性别必选'
		}else if(that.data.user.birth == null || that.data.user.birth ==''){
			msg='生日必选'
		}else if(that.data.user.height ==''){
			msg='身高必填'
		}else if(that.data.user.weight ==''){
			msg='体重必填'
		}else if(!RegExp.test(that.data.user.height)){
			msg='身高只能是正整数'
		}else if(!RegExp.test(that.data.user.weight)){
			msg='体重只能是正整数'
		}




		if(msg != ''){
			wx.showToast({
			  title: msg,
			  icon:'none',

			  duration: 3000
			})
		}else{
			wx.showLoading({
			  title: '处理中...',
			})

			setTimeout(function () {
			  wx.hideLoading()
			}, 5000)

			console.log('====>>待处理数据')
			console.log(that.data.user)
			//save
			if(that.data.user.userInfoId != null && that.data.user.userInfoId >0){
				//put
				api.updInfo(that.data.user,function(res){
					console.log(res)
					console.log('修改成功')
					utilH.countMaxHeart()
					api.findAllInfos(function(res){
						console.log(res)
						console.log('查询成功')
						wx.hideLoading()
					})
					that.toGeren()

				})
			}else{
				//add
				// api.addWxInfo(that.data.user,function(res){
				// 	console.log(res)
				// 	console.log('新增成功')
				// 	api.findAllWxInfo(function(res){
				// 		console.log(res)
				// 		console.log('查询成功')
				// 		wx.hideLoading()
				// 		that.toGeren()
				// 	})
				// })
			}
		}




	},
	uploadImg: function() {
		var that = this
		wx.chooseImage({
			success(res) {
				const tempFilePaths = res.tempFilePaths

				api.uploadFile(tempFilePaths[0], function(res) {
					that.setData({
						'user.avatarUrl': res
					})
				})


			}
		})
	},
	toGeren:function () {
		var len = getCurrentPages()
		console.log('====>>完成')
		console.log(len)
		wx.navigateBack({
			delta: 1
		})
	},
	toShuoming:function (e) {
		let value = e.currentTarget.dataset.val
		wx.navigateTo({
			url: "/pages/index/shuoming/shuoming?val="+value
		})
	}

})
